package com.xd.dubbo.globalTransaction.transactional;

import com.alibaba.fastjson.JSONObject;
import com.xd.dubbo.globalTransaction.netty.NettyClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
public class GlobalTransactionalManager {


    private static NettyClient nettyClient;

    private static ThreadLocal<GlobalTransaction> current = new ThreadLocal<>();
    private static ThreadLocal<String> currentGroupId = new ThreadLocal<>();
    private static ThreadLocal<Integer> transactionCount = new ThreadLocal<>();
    static {
        GlobalTransactionalManager.setCurrentGroupId("9729977f-5a3a-4d9d-a14d-7a8f678dcea0");
        GlobalTransactionalManager.setTransactionCount(2);
    }

    @Autowired
    public void setNettyClient(NettyClient nettyClient) {
        GlobalTransactionalManager.nettyClient = nettyClient;
    }

    public static Map<String, GlobalTransaction> GLOBALTRANSACTIONMAP = new HashMap<>();


    public static String createGlobalTransactionGroup() {
        String groupId = "9729977f-5a3a-4d9d-a14d-7a8f678dcea0";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("groupId", groupId);
        jsonObject.put("command", "create");
        nettyClient.send(jsonObject);
        System.out.println("创建事务组："+ jsonObject.toJSONString());
        currentGroupId.set(groupId);
        return groupId;
    }


    public static GlobalTransaction createGlobalTransaction(String groupId) {
        String transactionId = UUID.randomUUID().toString();
        GlobalTransaction globalTransaction = new GlobalTransaction(groupId, transactionId);
        GLOBALTRANSACTIONMAP.put(groupId, globalTransaction);
        current.set(globalTransaction);
        addTransactionCount();
        System.out.println("创建新的事务："+ globalTransaction.toString());
        return globalTransaction;
    }


    public static GlobalTransaction addGlobalTransaction(GlobalTransaction globalTransaction, Boolean isEnd, TransactionType transactionType) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("groupId", globalTransaction.getGroupId());
        jsonObject.put("transactionId", globalTransaction.getTransactionId());
        jsonObject.put("transactionType", transactionType);
        jsonObject.put("command", "add");
        jsonObject.put("isEnd", isEnd);
        jsonObject.put("transactionCount", GlobalTransactionalManager.getTransactionCount());
        nettyClient.send(jsonObject);
        System.out.println("向管理器添加事务：" + jsonObject.toJSONString());
        return globalTransaction;
    }

    public static GlobalTransaction getGlobalTransaction(String groupId) {
        return GLOBALTRANSACTIONMAP.get(groupId);
    }

    public static GlobalTransaction getCurrent() {
        return current.get();
    }
    public static String getCurrentGroupId() {
        return currentGroupId.get();
    }

    public static void setCurrentGroupId(String groupId) {
        currentGroupId.set(groupId);
    }

    public static Integer getTransactionCount() {
        return transactionCount.get();
    }

    public static void setTransactionCount(int i) {
        transactionCount.set(i);
    }

    public static Integer addTransactionCount() {
        int i = (transactionCount.get() == null ? 0 : transactionCount.get()) + 1;
        transactionCount.set(i);
        return i;
    }
}
